package com.example.ruteroelmeromero;

import java.util.List;
import java.util.Map;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.OverlayItem;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

public class RuteroElMeroMero extends MapActivity implements LocationListener {
	BaseDatos db = null;
	private MapController mapController;
	private MapView mapa = null;
	private LocationManager locationManager;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_rutero_el_mero_mero);
		if (db == null) {
			db = new BaseDatos(this);
		}
		// Obtenemos una referencia al control MapView
		mapa = (MapView) findViewById(R.id.mapa);

		// Mostramos los controles de zoom sobre el mapa
		mapa.setBuiltInZoomControls(true);
		mapController = mapa.getController();
		mapController.setZoom(17);
		llenado();
	}

	public void llenado() {
		locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
		// default
		Location location = null;
		locationManager.requestLocationUpdates(
				LocationManager.NETWORK_PROVIDER, 5000, 10, this);
		location = locationManager
				.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

		// Initialize the location fields
		if (location != null) {
			onLocationChanged(location);
		} else {
			onLocationChanged(location);
			System.out.println("NO");
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_rutero_el_mero_mero, menu);
		return true;
	}

	/* Remove the locationlistener updates when Activity is paused */
	@Override
	protected void onPause() {
		super.onPause();
		locationManager.removeUpdates(this);
	}

	@Override
	protected boolean isRouteDisplayed() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public void onLocationChanged(Location location) {
		double lat = 14.624794770181;
		double lng = -90.5167743226929;
		// double lat = (double) (location.getLatitude());
		// double lng = (double) (location.getLongitude());
		List mapOverlays = mapa.getOverlays();
		mapOverlays.clear();
		if (db == null) {
			db = new BaseDatos(this);
		}
		List<Map> listado = db.listar(15, lat, lng);
		Drawable drawable = this.getResources().getDrawable(
				R.drawable.bailarina);
		CustomItemizedFactory factory = new CustomItemizedFactory(drawable,
				this);
		for (Map punto : listado) {
			CustomItemizedOverlay itemizedOverlay = factory.getItem(
					(int) (((Double) punto.get("lat")).doubleValue() * 1e6),
					(int) (((Double) punto.get("lng")).doubleValue() * 1e6),
					(String) punto.get("tienda"),
					(String) punto.get("ubicacion"));
			mapOverlays.add(itemizedOverlay);
		}
		drawable = this.getResources().getDrawable(R.drawable.persona_mini);
		CustomItemizedOverlay itemizedOverlay1 = new CustomItemizedOverlay(
				drawable, this);
		GeoPoint point1 = new GeoPoint((int) (lat * 1e6), (int) (lng * 1e6));
		OverlayItem overlayitem1 = new OverlayItem(point1, "AkiToy", "AkiToy");
		itemizedOverlay1.addOverlay(overlayitem1);
		mapOverlays.add(itemizedOverlay1);
		MapController mapController = mapa.getController();

		mapController.animateTo(point1);
	}

	@Override
	public void onProviderDisabled(String provider) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onProviderEnabled(String provider) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onStatusChanged(String provider, int status, Bundle extras) {
		// TODO Auto-generated method stub

	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle item selection
		switch (item.getItemId()) {
		case R.id.menu_settings:
			Intent intent = new Intent(this, DetalleVentas.class);
			this.startActivity(intent);
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}

}
